package com.zchat.android.tasks;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.packet.Presence;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.zchat.android.ChatActivity;
import com.zchat.android.WelcomeActivity;
import com.zchat.android.adapters.RosterAdapter;
import com.zchat.android.service_aidl_beans.User;
import com.zchat.api.ZChat;

/**
 * 测试Task
 * @author Oliverzhu
 *	2012/6/19
 */
public class LoadTestTask extends AsyncTask<Object, Integer, Object> {
	private Context context;
	
	private ZChat zchat;
	
	private ListView listView;
	
	private Roster roster;
	
	@Override
	protected Object doInBackground(Object... params) {
		context = (Context) params[0];
		zchat = (ZChat) params[1];
		listView = (ListView) params[2];
		
//		Map<String, Object> map = zchat.getTestDatas();
		
		
		List<User> userinfos = new ArrayList<User>();
		roster = zchat.getXMPPConnection().getRoster();
		
		Collection<RosterEntry> entries = roster.getEntries();
		for (RosterEntry entry : entries) {
			System.out.print(entry.getName() + " - " + entry.getUser() + " - "
					+ entry.getType() + " - " + entry.getGroups().size());
			Presence presence = roster.getPresence(entry.getUser());
			System.out.println(" - " + presence.getStatus() + " - "
					+ presence.getFrom());
			User user = new User();
			user.setName(entry.getName());
			user.setUserName(entry.getUser());
			user.setType(entry.getType());
			user.setSize(entry.getGroups().size());
			user.setStatus(presence.getStatus());
			user.setFrom(presence.getFrom());

			userinfos.add(user);
		}
		
		return userinfos;
	}
	
	@Override
	protected void onPostExecute(Object result) {
		super.onPostExecute(result);
		
		final ArrayList<User> userInfos = (ArrayList<User>) result;
//		Map map = (Map) result;
//		TextView tv = (TextView) v.findViewById(R.id.tv);
//		
//		TestBean tb = (TestBean) map.get("test");
//		tv.setText(tb.getId() + tb.getName());
		
		// 添加花名册监听器，监听好友状态的改变。
		roster.addRosterListener(new RosterListener() {

			@Override
			public void entriesAdded(Collection<String> addresses) {
				System.out.println("entriesAdded");
			}

			@Override
			public void entriesUpdated(Collection<String> addresses) {
				System.out.println("entriesUpdated");
			}

			@Override
			public void entriesDeleted(Collection<String> addresses) {
				System.out.println("entriesDeleted");
			}

			@Override
			public void presenceChanged(Presence presence) {
				System.out
						.println("presenceChanged - >" + presence.getStatus());
			}

		});
		
		RosterAdapter rosterAdapter = new RosterAdapter(context, userInfos);
		
		listView.setAdapter(rosterAdapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long id) {
				// 获取用户，跳到聊天室
				User user = userInfos.get(position);
				Intent intent = new Intent(context,
						ChatActivity.class);
				intent.putExtra("user", user.getUserName());
				context.startActivity(intent);

			}

		});
	}

}
